﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.IO;
using System.Configuration;

public partial class member_ChangeActive : System.Web.UI.Page
{
	public string EnterpriseName = "";
    public string UserID = "";
    public string UserPower = "";
    public string Footer = "";

    public string dirPath = ConfigurationManager.AppSettings["dirPath"].ToString();
	
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Init.islogin(ref UserID,ref EnterpriseName,ref UserPower))
        {
            Response.Redirect("/Login.aspx");
        }
        
        if (!UserPower.Contains("Manager"))
        {
            Response.Write("你没有权限！");
            Response.End();
        }
        this.EnableViewState = false;        
        Response.Write(Init.InitDefault());
    }

    Common Init = new Common();
    MsSqlHelper mssql = new MsSqlHelper();
    public void list(string spinfo, string pihao)
    {
        DataSet myds = DataG(spinfo, pihao);
       GridView1.DataSource = myds;
       GridView1.DataKeyNames = new string[] { "imageid" };//主键
       GridView1.DataBind();
    }

    public void bind()
    {
        DataSet myds = DataG("", "");
        DataTable mydt = myds.Tables[0];

        for (int i = 0; i < mydt.Rows.Count; i++)
        {
            DataRow row = mydt.Rows[i];
           // row[i]["isfile"] = 1;//统一对含_time的属性设置值.
        }

        GridView1.DataSource = myds;
        GridView1.DataKeyNames = new string[] { "imageid" };//主键
        GridView1.DataBind();
    }

    //编辑
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        list(spinfo.Text, pihao.Text);
       //bind();
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if ((e.Row.RowState == (DataControlRowState.Edit | DataControlRowState.Alternate)) || (e.Row.RowState == DataControlRowState.Edit))
        {
            TextBox curText;
            
            for (int i = 2; i < 7; i++)
            {
                curText = (TextBox)e.Row.Cells[i].Controls[0];
                curText.TextMode = TextBoxMode.MultiLine;
                curText.Attributes.Add("style ", "width:80%;overflow:visible ");
            }            
        }
    }
    //删除
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        Response.Write( GridView1.DataKeys[e.RowIndex].Value.ToString() );

        string imageid = GridView1.DataKeys[e.RowIndex].Value.ToString();
        imagedocupdate(imageid);
        list(spinfo.Text, pihao.Text);
    }

    //删除
    protected void GridView1_RowDeletingxxx(object sender, GridViewDeleteEventArgs e)
    {
        string sqlstr = "delete from 表 where id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
        mssql.ExecuteSql(sqlstr);
    }

    //更新
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string sqlstr = "update 表 set 字段1='"
            + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',字段2='"
            + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',字段3='"
            + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "' where id='"
            + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
        mssql.ExecuteSql(sqlstr);
        GridView1.EditIndex = -1;
        list(spinfo.Text, pihao.Text);
    }
    //取消
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        list(spinfo.Text, pihao.Text);
    }

    //分页
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        list(spinfo.Text, pihao.Text);
        //GridView1.DataBind();
    }

    protected void Submit_Click(object sender, EventArgs e)
    {
       list(spinfo.Text, pihao.Text);
    }

    //展示
    public DataSet DataG(string spinfo, string pihao)
    {
        string sql = "select top 1000 imageid,spid,rtrim(spbh) as spbh,rtrim(spmch) as spmch,rtrim(shpgg) as shpgg,rtrim(dw) as dw,rtrim(shengccj) as shengccj,"
                +"rtrim(fzxx1) as fzxx1,rtrim(fzxx2) as fzxx2,rtrim(fzxx3) as fzxx3,rtrim(fzxx4) as fzxx4,rtrim(fzxx5) as fzxx5,url "
                //+ "'"+isfile("+url+")+"'"
                + "from [ImageDoc] a(nolock),spkfk b(nolock) "
                + "where a.beactive='是' and a.detailid=b.spid and ( spbh like '%" + spinfo + "%' or spmch like '%" + spinfo + "%' or zjm like '%" + spinfo + "%') and a.fzxx1 like '%" + pihao + "%' "
                + "order by spid,fzxx1,fzxx2,fzxx3,fzxx4,fzxx5,imageid";
        if (Init.IsDebug)
        {
            ResultDeBug.Text = sql;
        }
        DataSet dss = mssql.ExecuteDs(sql);
        return dss;
    }
    //更新
    public void imagedocupdate(string imageid)
    {
        string sql = "update [ImageDoc] set beactive='否',fzxx5='删除时间'+convert(varchar(100),getdate(),21) where imageid='" + imageid + "'";
        mssql.ExecuteSql(sql);
    }

    public bool isfile(string id, string basePath = "~/Upload/Images/")
    {
        basePath = (basePath.IndexOf("~") > -1) ? HttpContext.Current.Server.MapPath("~") :
        basePath;
        string FilePath = "";
        FilePath = mssql.GetString("select rtrim(url) as url from [ImageDoc](nolock) where imageid='" + id + "'");
        if (File.Exists(basePath + FilePath.Replace("upload/", "")))
        {
            return true;
        }
        else
        {
            if (File.Exists(dirPath + FilePath.Replace("Upload/", "")))
            {
                return true;
            }
            else
            {
                    return false;
            }
        }
    }  
}

